/*This activity contains the chat fragment. 
hello!!!!
 * The layout is in the chat.xml file.
 * This file describe what happens when you select an item from the ContactListFragment. */
package dk.aau.walkietalkieui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import dk.aau.walkietalkie.picture.MyCamera;
import dk.aau.walkietalkie.picture.SendPicture;
import dk.aau.walkietalkieui.adapter.ChatMessageAdapter;
import dk.aau.walkietalkieui.fragment.ChatFragment;
import dk.aau.walkietalkieui.fragment.ChatFragment.IMessageListFragment;
import dk.aau.walkietalkieui.message.MyMessage;

public class ChatActivity extends Base_FragmentActivity implements
		IMessageListFragment {

	public final static String KEY_SELECTED_INDEX = "selIndex";
	private static final String SELECTED_NAME = "SelectedName";
	private static final String SELECTED_IP = "SelectedIp";
	public final String TAG = "ChatActivity";
	private MyCamera cam;
	public static final int REQUEST_SEND_IMAGE = 3;
	static final int NUM_ITEMS = 10;
	private static final int SELECT_PICTURE = 1;
	private static final int RESULT_LOAD_IMAGE = 1;
	public static final String IMAGE_DATA = "image data";

	//private static ChatMessageAdapter adapterChat;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat);

		if (savedInstanceState == null) {
			Bundle extras = getIntent().getExtras();

			if (extras != null && extras.containsKey(KEY_SELECTED_INDEX)
					&& extras.containsKey(SELECTED_NAME)
					&& extras.containsKey(SELECTED_IP)) {
				int selIndex = extras.getInt(KEY_SELECTED_INDEX);
				String name = extras.getString(SELECTED_NAME);
				String ip = extras.getString(SELECTED_IP);

				FragmentManager fm = getSupportFragmentManager();

				FragmentTransaction ft = fm.beginTransaction();
				ft.replace(R.id.ChatFragmentContent,
						ChatFragment.newInstance(selIndex, name, ip),
						ChatFragment.TAG);
				ft.commit();
			} else {
				Log.e(TAG, "No selected index supplied!");
				finish();
			}
		}
	}

	// resource is the xml file of the popup window
	// buttonId is the dismiss button of the popup window
	// v is the view to which the popup window is attached to
	// act is the activity for taking a picture(used in the takePhoto method of
	// MyCamera
	public void showPopupWindowPhoto(int resource, int buttonId, View v,
			Activity act) {
		// Create a popup that has multiple selection options
		final Activity activity = act;

		CharSequence parameters[] = new CharSequence[] {
				act.getString(R.string.send_picture_menu_take_picture),
				act.getString(R.string.send_picture_menu_gallery),
				act.getString(R.string.send_picture_menu_cancel_choose) };
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.send_picture_menu_title);
		builder.setItems(parameters, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// the user clicked on colors[which]

				switch (which) {
				case 0: {
					cam = new MyCamera();
					cam.takePhoto(activity);
					break;
				}
				case 1: {
					Intent i = new Intent(
							Intent.ACTION_PICK,
							android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
					startActivityForResult(i, RESULT_LOAD_IMAGE);
					break;
				}

				}
			}
		});
		builder.show();

	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.v("ChatFragment", "requestCode" + requestCode + "resultCode"
				+ resultCode);
	if (requestCode == MyCamera.CAM_REQUEST && resultCode != Activity.RESULT_CANCELED) {
			ChatFragment.sendMessage(MyCamera.getImagePath(), System.currentTimeMillis(),
					MyMessage.MessageType.TYPE_PICTURE);
			
		} else if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
				&& null != data) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };
			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();
			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);
			cursor.close();
			ChatFragment.sendMessage(picturePath, System.currentTimeMillis(),
					MyMessage.MessageType.TYPE_PICTURE);
		} else {
			super.onActivityResult(requestCode, resultCode, data);
		}

	}

	public void showImage(String picturePath) {
		Intent i = new Intent();
		i.setClass(getApplicationContext(), SendPicture.class);
		i.putExtra(IMAGE_DATA, picturePath);
		startActivityForResult(i, REQUEST_SEND_IMAGE);
	}

	@Override
	public void onMessageItemSelected(int position) {
		Toast.makeText(getApplicationContext(),
				"Message number " + position + " selected", Toast.LENGTH_SHORT)
				.show();

	}
	
	@Override
	public Object onRetainCustomNonConfigurationInstance() {
		return ChatFragment.getChatAdapter();
	}
	

}
